﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data_Access_Layer;
namespace Business_Layer
{
	public class EmployeeController
	{
		public string _err = "";
		/// <summary>
		/// hàm lấy tất cả các nhân viên
		/// </summary>
		/// <returns></returns>
		public List<Employee> GetAll()
		{
			return Employee.Objects.Where(x => x.Deleted == false).ToList();
		}
		/// <summary>
		/// Hàm xóa thông tin nhân viên
		/// </summary>
		/// <param name="emp"></param>
		/// <returns></returns>
		public bool Delete(Employee emp)
		{
			Employee.Objects.Update(emp);
			return true;
		}
		/// <summary>
		/// Hàm thêm và cập nhật thông tin nhân viên.
		/// </summary>
		/// <param name="emp"></param>
		/// <returns></returns>
		public bool Insert_Update(Employee emp)
		{
			try
			{
				if (emp.Id > 0)
				{
					Employee.Objects.Update(emp);
					return true;
				}
				else
				{
					Employee.Objects.Insert(emp);
					return true;
				}
			}
			catch (Exception ex)
			{
				_err = ex.Message;
				return false;
			}
			
			
		}
		public Employee FindById(int id) 
		{
			return Employee.Objects.FirstOrDefault(x => x.Id == id && x.Deleted == false);
		}
		public List<Employee> FindByName(string name)
		{
			return Employee.Objects.Where(x => x.Name.IndexOf(name)!=-1 && x.Deleted == false).ToList();
		}
		public List<Employee> FindByRole(int role)
		{
			return Employee.Objects.Where(x => x.RoleId==role && x.Deleted == false).ToList();
		}

	}
}
